import Service from '../../core/baseService';
import { getCurrentStimp, yuanToFen } from '../../utils'

export default class GoodsService extends Service {
  public GoodsGallery: any;
  constructor(ctx) {
    super(ctx);
    this.model = ctx.sassmodel.Goods;
    this.GoodsGallery = ctx.sassmodel.GoodsGallery;
  }

  async list(page = 1, pageSize = 20, query) {
    let total = 0;
    let queryObj = { ...query };

    const result = await this.model
      .findAndCountAll({
        where: queryObj,
        include: [
          {
            model: this.ctx.model.Category,
          },
          {
            model: this.ctx.model.GoodsSku
          },
          {
            model: this.ctx.model.GoodsAttr,
            include: {
              model: this.ctx.model.GoodsAttrVal
            }
          }
        ],
        offset: (Number(page) - 1) * pageSize,
        limit: pageSize,
        order: [['id', 'DESC']],
      });

    return {
      list: result.rows,
      total,
      page,
      pageSize,
    };
  }

  async create(data) {
    const { banner } = data;
    const stemp = getCurrentStimp();
    Object.assign(data, {
      create_at: stemp,
      update_at: stemp,
    });
    delete data.banner;

    const toData = yuanToFen(data, ['cost_price', 'market_price', 'price']);
    const result = await this.model.create(toData);
    // 插入banner
    const ans: any = [];
    banner.forEach(({ url }) => {
      ans.push({
        url,
        goods_id: result.id
      });
    });
    await this.GoodsGallery.bulkCreate(ans);

    return result;
  }


  async detail({ id, user_id }) {

    const whereObj = { goods_id: id };
    if (user_id) {
      Object.assign(whereObj, { user_id });
    }

    const result = await this.model.findOne({
      where: { id },
      include: [
        {
          attributes: ['id', 'url'],
          as: 'banner',
          model: this.ctx.model.GoodsGallery,
        },
      ],
    });
    return result;
  }


  async update(goods_id, data) {
    try {
      // 插入gallery
      const ans: any = [];
      const { banner } = data;
      banner.forEach(({ url, id }) => {
        ans.push({
          id,
          goods_id,
          url
        });
      });


      const toData = yuanToFen(data, ['cost_price', 'market_price', 'price']);

      await this.GoodsGallery.bulkCreate(ans, { updateOnDuplicate: ['id'] });
      return await this.model
        .update(
          { ...toData },
          { where: { id: goods_id } }
        )
    } catch (err: any) {
      this.ctx.logger.error(err.message);
      return '';
    }
  }


  async goodsByCategory(page = 1, pageSize = 20, category_id) {
    const result = await this.model
      .findAndCountAll({
        where: { category_id },
        offset: (Number(page) - 1) * pageSize,
        limit: pageSize,
        order: [['id', 'DESC']],
      });

    console.log(result);

    return {
      list: result.rows,
      total: result.count,
      page,
      pageSize,
    };
  }

}
